<?php
/**
 * Created by MY_MIND.
 * User: WDJ
 * Date: 2018/7/18 0018
 * Time: 16:58
 */
namespace app\admin\controller;

use \think\Db;
use \think\Session;

class Record extends Permissions
{

    /**
     * 查看所有的订单记录
     */
    public function shopping()
    {
        /**
         * 拼接查询条件
         */
        $get    =   $this->request->get();
        $where  =   [];
        if(isset($get['uid']) && $uid = $get['uid']){
            $where['uid']   =   intval($uid);
        }
        //订单开始时间
        if(isset($get['start_time']) && $start_time = $get['start_time']){
            $where['create_time']   =   ['egt', strtotime($start_time)];
        }
        //订单结束时间
        if(isset($get['end_time']) && $end_time = $get['end_time']){
            $where['create_time']   =   isset($where['create_time']) ? ['between', [$where['create_time'][1] , strtotime($end_time)]] : ['elg', strtotime($end_time)];
        }

        $count      =   Db::name('shop_log')->where($where)->count();
        $result     =   Db::name('shop_log')->where($where)->order('pay_time desc')->paginate(10, $count);
//        dump(Db::name('shop_log')->getLastSql());
        $this->assign(['result' => $result]);
        return $this->fetch();
    }

    /**
     * 查看所有的微信充值记录
     */
    public function wxcharge()
    {

    }

    /*
     * 房卡消耗记录
     */
    public function home()
    {
        $res = Session::get("admin_cate_id");
        $ress = Session::get("admin");
        $resa = Db::name('admin')->where('id',$ress)->value('name');
        $resb = Db::name('agent')->where('phone',$resa)->value('id');

        $post = $this->request->param();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['UserID'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if($res==1){
            $list = empty($where) ? getSqlConnec('QPTreasureDB_CHUI', 'RecordFangKaCost')->order('ID desc')->paginate(20) : getSqlConnec('QPTreasureDB_CHUI', 'RecordFangKaCost')->order('ID desc')->where($where)->paginate(20,false,['query'=>$this->request->param()]);


        }else{
            $code = Db::name('ratio')->where('agent_id',$resb)->value('request');
            $user = Db::name('relation')->where('invitation_code',$code)->select();
            foreach ($user as $key => $value) {
                $uid = $value['game_user_id'];
                $list = getSqlConnec('QPTreasureDB_CHUI', 'RecordFangKaCost')->where("UserID=$uid")->order('ID desc')->paginate(20);

            }
            if(!$user){
                $list = Db::name('ratio')->where('agent_id',0)->paginate(20);;
            }
        }
        $this->assign('list', $list);
        return $this->fetch();
    }

    /*
     * 币换卡记录
     **/
    public function change()
    {
        $res = Session::get("admin_cate_id");
        $ress = Session::get("admin");
        $resa = Db::name('admin')->where('id',$ress)->value('name');
        $resb = Db::name('agent')->where('phone',$resa)->value('id');


        $post = $this->request->param();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['user_id'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if($res==1){
            $list = empty($where) ? Db::name('goldhome')->order('id desc')->paginate(20) : Db::name('goldhome')->order('id desc')->where($where)->paginate(20,false,['query'=>$this->request->param()]);
        }else{
            $list = empty($where) ? Db::name('goldhome')->where('agent_id',$resb)->order('id desc')->paginate(20) : Db::name('goldhome')->where('agent_id',$resb)->order('id desc')->where($where)->paginate(20,false,['query'=>$this->request->param()]);

        }

        $this->assign('list', $list);

        return $this->fetch();
    }

    public function delete()
    {
        $id = input('id');
        $lis = Db::name('goldhome')->where('id',$id)->delete();
        if($lis){
            return $this->success('删除成功','admin/record/index');
        }else{
            return $this->error('删除失败');
        }

    }
    public function del()
    {
        $id = input('id');
        $list = Db::connect("QPTreasureDB")->table('RecordFangKaCost')->where("ID='$id'")->where("KindID=600")->where("CostType=1")->paginate(20);
        if($list){
            return $this->success('删除成功','admin/record/index');
        }else{
            return $this->error('删除失败');
        }

    }

}
